package com.google.code.jaail.networks

import com.google.code.jaail.networks.protocols.ChatProtocol
import org.junit.Before
import org.junit.Test
import static junit.framework.Assert.fail
import static org.mockito.Mockito.*

class ChatNetworkTest {
    ChatProtocol mockProtocol
    ChatNetwork network
    Socket mockSocket
    InetSocketAddress mockInetSocketAddress
    InputStream mockInputStream
    OutputStream mockOutputStream
    InetSocketAddressHelper mockHelper

    @Before
    void setUpMocks() {
        mockInputStream = mock(InputStream)
        mockOutputStream = mock(OutputStream)
        mockInetSocketAddress = mock(InetSocketAddress)
        mockProtocol = mock(ChatProtocol)
        mockSocket = mock(Socket)
        mockHelper = mock(InetSocketAddressHelper);
        network = new ChatNetwork("someProtocol", mockHelper, mockSocket, mockProtocol)
    }

    @Test
    void "test a chat network connects to the supplied port and address"() {
        network.setPortSetting(1010)

        when(mockHelper.lookupInetSocketAddress(1010)).thenReturn(mockInetSocketAddress);
        when(mockSocket.getInputStream()).thenReturn(mockInputStream);
        when(mockSocket.getOutputStream()).thenReturn(mockOutputStream);

        network.connect()

        verify(mockSocket).connect(mockInetSocketAddress)
        verify(mockProtocol).negotiateConnection(mockOutputStream, mockInputStream)
    }

    @Test
    void "test disconnecting"() {
        when(mockSocket.isConnected()).thenReturn(true)

        network.is = mockInputStream
        network.os = mockOutputStream
        network.disconnect()
        verify(mockInputStream).close()
        verify(mockOutputStream).close()
        verify(mockSocket).close()
        verify(mockProtocol).negotiateDisconnect(mockOutputStream, mockInputStream)
    }

    @Test
    void "The network must deliver messages to the intended recipient"() {
        fail("implement me")
    }
}
